// pages/miners/miners.js
import api from "../../utils/apis.js";
import message from "../../utils/message.js";
import util from "../../utils/util.js";

Page({
  data: {
    minerId: '',
    minerNo: '',
    headData: '',
    daemon: '',
    core: '',
    protocol: '',
    cpu: '',
    Mem: '',
    Disk: '',
    system: '',
    hardware: '',
    mem_total: '',
    mem_free: '',
    all: '',
    free: '',
    checkPropertyData: [],
    nodeId: ''
  },

  onLoad: function(options) {
    this.setData({
      minerId: options.minerId
    });

    this.getMinerDetail(this.data.minerId);
  },

  getMinerDetail(minerid) {
    let self = this;

    let token = wx.getStorageSync("authorization");
    wx.showLoading({
      title: "加载中"
    });

    wx.request({
      url: api.minerDetail + minerid,
      method: "GET",
      header: {
        Authorization: "Bearer " + token
      },
      success(res) {
        wx.hideLoading();

        if (res.statusCode == 200) {
          let data = res.data;

          self.setData({
            minerNo: data.minerNo,
          })

          self.chuli(data);

          let checkProperty = data.nodeList;

          for (let i = 0; i < checkProperty.length; i++) {
            checkProperty[i].nodeInfo = JSON.parse(checkProperty[i].nodeInfo);
            checkProperty[i].nodeId = util.hideWord(checkProperty[i].nodeId);
          }

          self.setData({
            checkPropertyData: checkProperty
          });
        } else {
          util.errHandler(res);
        }
      }
    });
  },

  chuli(element) {
    let jsonSytem = {};
    let jsonHardware = {};

    if (element.systemInfo) {
      let temp = util.replaceAll(element.systemInfo, "\\\\", "");
      if(JSON.parse(temp)) {
        jsonSytem = JSON.parse(temp);
      }
    }

    if (element.hardwareInfo) {
      let temp = util.replaceAll(element.hardwareInfo, "\\\\", "");
      if (JSON.parse(temp)) {
        jsonHardware = JSON.parse(temp);
      }
    }

    let a = jsonSytem.storj_version;
    let arry = a.split(",");
    let daemon_1 = arry[0];
    let core_1 = arry[1];
    let protocol_1 = arry[2];
    let cpu_1 = jsonHardware.Cpu.processors[0].model_name;

    this.setData({
      system: jsonSytem,
      hardware: jsonHardware,
      daemon: jsonHardware,
      core: core_1,
      protocol: protocol_1,
      cpu: cpu_1,
      mem_total: (jsonHardware.Mem.mem_total / 1024.0 / 1024.0).toFixed(2),
      mem_free: (jsonHardware.Mem.mem_free / 1024.0 / 1024.0).toFixed(2),
      all: (jsonHardware.Disk.all / 1024.0 / 1024.0).toFixed(2),
      free: (jsonHardware.Disk.free / 1024.0 / 1024.0).toFixed(2)
    });
  }
});
